#include "GameModel.h"
#include <time.h>
#include <stdlib.h>

#include <QStringRef>
#include <QUrl>
#include "mainwindow.h"
GameModel::GameModel()
{

}

void GameModel::startGame(GameType type){
    gameType = type;
    //初始化棋盤
    gameMapVec.clear();
    for(int i=0;i<BOARD_GRAD_SIZE;i++){
        std::vector<int> lineBoard;
        for(int j=0;j<BOARD_GRAD_SIZE;j++){
            lineBoard.push_back(0);
        }
        gameMapVec.push_back(lineBoard);
    }
    //如果是AI模式，需要初始化評分數組
    if(gameType == AI)
    {
        scoreMapVec.clear();
        for(int i=0;i<BOARD_GRAD_SIZE;i++){
            std::vector<int> lineScores;
            for(int j=0;j<BOARD_GRAD_SIZE;j++){
                lineScores.push_back(0);
            }
            scoreMapVec.push_back(lineScores);
        }
    }
    //輪到黑方下棋為true，反之
    playerFlag = true;
}

void GameModel::actionByPerson(int row, int col)
{
    updateGameMap(row,col);
    // /////////////////////////////////////////////////////////////////////////////////////
    if(gameType == net)
    {
        //sent mess to server
        auto x = QString::number(row,10),
             y = QString::number(col,10);

           QString url = "http://43.134.52.25:30000/submit?x="+x+"&y="+y+"&userID="+userID;
           request.setUrl(QUrl(url));
           manager->get(request);
    }
}
void GameModel::updateGameMap(int row, int col)
{
    if(playerFlag)
    {
        gameMapVec[row][col] = 1;
    }
    else
    {
        gameMapVec[row][col] = -1;
    }
    //换手
    playerFlag = !playerFlag;
}

bool GameModel::isWin(int row,int col)
{
    /*判斷下棋點的水平、垂直、左斜、右斜方中
     * 有沒有5子相連的情況，如有則贏
     */
    for(int i=0;i<5;i++){
        if(row>0 && row<BOARD_GRAD_SIZE &&
           col-i>0 && col-i+4<BOARD_GRAD_SIZE &&
           gameMapVec[row][col-i] == gameMapVec[row][col-i+1] &&
           gameMapVec[row][col-i] == gameMapVec[row][col-i+2] &&
           gameMapVec[row][col-i] == gameMapVec[row][col-i+3] &&
           gameMapVec[row][col-i] == gameMapVec[row][col-i+4]){
            return true;
        }
        if(row-i>0 && row-i+4<BOARD_GRAD_SIZE &&
           col>0 && col<BOARD_GRAD_SIZE &&
           gameMapVec[row-i][col] == gameMapVec[row-i+1][col] &&
           gameMapVec[row-i][col] == gameMapVec[row-i+2][col] &&
           gameMapVec[row-i][col] == gameMapVec[row-i+3][col] &&
           gameMapVec[row-i][col] == gameMapVec[row-i+4][col]){
            return true;
        }

        if(
           row-i>0 && row-i+4<BOARD_GRAD_SIZE &&
           col+i-4>0 && col+i<BOARD_GRAD_SIZE &&
           gameMapVec[row-i][col+i] == gameMapVec[row-i+1][col+i-1] &&
           gameMapVec[row-i][col+i] == gameMapVec[row-i+2][col+i-2] &&
           gameMapVec[row-i][col+i] == gameMapVec[row-i+3][col+i-3] &&
           gameMapVec[row-i][col+i] == gameMapVec[row-i+4][col+i-4])
        {
            return true;
        }

        if(row-i>0 && row-i+4<BOARD_GRAD_SIZE &&
           col-i>0 && col-i+4<BOARD_GRAD_SIZE &&
           gameMapVec[row-i][col-i] == gameMapVec[row-i+1][col-i+1] &&
           gameMapVec[row-i][col-i] == gameMapVec[row-i+2][col-i+2] &&
           gameMapVec[row-i][col-i] == gameMapVec[row-i+3][col-i+3] &&
           gameMapVec[row-i][col-i] == gameMapVec[row-i+4][col-i+4]){
            return true;
        }

    }
    return false;
}


void GameModel::actionBynet(int &clickRow,int &clickCol)
{
    // /////////////////////////////////////////////////////////////////////////////////
    //get message from server
    QString url = "http://43.134.52.25:30000//getOperationLists";
       request.setUrl(QUrl(url));
       manager->get(request);
    updateGameMap(clickRow,clickCol);

}
